package lt.startup.duobes;

import java.security.MessageDigest;
import java.util.Random;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.preference.PreferenceManager;
import android.util.Log;

import com.crazyimp.utilities.Downloader;
import com.google.android.maps.MapView;

public class Constants {
	
	public static final String LOG = "LOG";
	public static final String SAVE_SENSE = "save_hw";
	public static final String SAVE_ID = "save_id";
	public static float sence;
	public static String id;
	public static Downloader downloader = new Downloader();
	
	public static MapView map;
	public static Location location;

	
	public static float MIN_DISTANCE = 5f;
	
	public static void savePreferences(Context act) {
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(act);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putFloat(Constants.SAVE_SENSE,  Constants.sence);
		editor.putString(SAVE_ID, Constants.id);
		editor.commit();
	}
	
	public static void loadPreferences(Context act) {
		  SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(act);
		  Constants.sence = sharedPreferences.getFloat(Constants.SAVE_SENSE, 6f);
		  Constants.id = sharedPreferences.getString(SAVE_ID, getDeviceId(act));		  
	}
	
	private static String getDeviceId(Context act){
		Random random  = new Random(System.currentTimeMillis());
		String id = ""+random.nextLong();
		id = md5(id);
		Log.i("ID", id);
		Constants.id = id;
		savePreferences(act);
		return id;
	}
	
	public static String md5(String s) {
	    try {	        
	        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
	        digest.update(s.getBytes());
	        byte messageDigest[] = digest.digest();
	        
	        StringBuffer hexString = new StringBuffer();
	        for (int i=0; i<messageDigest.length; i++)
	            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
	        return hexString.toString();
	        
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return "";
	}
	
	
	
}
